package spring.cloud.tasks.client.batch_task_processor;

import lombok.Getter;
import lombok.Setter;
import spring.cloud.tasks.client.BatchTaskWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class BatchTaskExecuter {
    @Setter
    @Getter
    private boolean needStop = false;//自己主动检查需要将自己的线程终止
    @Setter
    @Getter
    private boolean stop = false;//外部指令将自己的线程终止

    //
    public String pingpongOrReportStatus(BatchTaskWorker batchTaskWorker, BatchTaskInstanceExecuterThreadInfo batchTaskInstanceExecuterThreadInfo) {
        Map<String, BatchTaskInfo> taskIdToBatchTaskInfoMap = batchTaskWorker.getTaskIdToBatchTaskInfoMap();
        if (taskIdToBatchTaskInfoMap == null) {
            setNeedStop(true);
            return null;
        }
        BatchTaskInfo batchTaskInfo = taskIdToBatchTaskInfoMap.get(batchTaskInstanceExecuterThreadInfo.getTaskId());
        if (batchTaskInfo == null) {
            setNeedStop(true);
            return null;
        }
        if (batchTaskInfo.isStop()) {
            setStop(true);//外部指令关闭
            return null;
        }
        BatchTaskWorkerInstanceContext batchTaskWorkerInstanceContext = batchTaskInfo.taskInstanceIdToWorkerTaskBatchInfoMap.get(batchTaskInstanceExecuterThreadInfo.getThreadId());
        if (batchTaskWorkerInstanceContext == null) {
            setNeedStop(true);
            return null;
        }
        BatchTaskWorkerInstanceClientInfo batchTaskWorkerInstanceClientInfo = batchTaskWorkerInstanceContext.getBatchTaskWorkerInstanceClientInfo();
        if (batchTaskWorkerInstanceClientInfo == null) {
            setNeedStop(true);
            return null;
        }
        if (BatchCreater.class.getName().equals(batchTaskInstanceExecuterThreadInfo.getThreadType())) {
            BatchTaskWorkerInstanceClientInfo.TaskThreadInfo taskThreadInfo = batchTaskWorkerInstanceClientInfo.getTaskCreaterThreadInfo();
            if (taskThreadInfo == null) {
                setNeedStop(true);
                return null;
            }
            if (taskThreadInfo.getThreadId().equals(batchTaskInstanceExecuterThreadInfo.getThreadId())) {
                taskThreadInfo.setLastUpdateDate(new Date());
                taskThreadInfo.setStatus(batchTaskInstanceExecuterThreadInfo.getStatus());
                return null;
            }
            setNeedStop(true);
            return null;
        } else if (BatchExecuter.class.getName().equals(batchTaskInstanceExecuterThreadInfo.getThreadType())) {
            List<BatchTaskWorkerInstanceClientInfo.TaskThreadInfo> taskThreadInfoList = batchTaskWorkerInstanceClientInfo.getTaskExecuterThreadInfoList();
            for (BatchTaskWorkerInstanceClientInfo.TaskThreadInfo taskThreadInfo : taskThreadInfoList) {
                if (taskThreadInfo != null && taskThreadInfo.getThreadId().equals(batchTaskInstanceExecuterThreadInfo.getThreadId())) {
                    taskThreadInfo.setLastUpdateDate(new Date());
                    taskThreadInfo.setStatus(batchTaskInstanceExecuterThreadInfo.getStatus());
                    return null;
                }
            }
            setNeedStop(true);
            return null;
        } else if (BatchResultCollecter.class.getName().equals(batchTaskInstanceExecuterThreadInfo.getThreadType())) {
            BatchTaskWorkerInstanceClientInfo.TaskThreadInfo taskThreadInfo = batchTaskWorkerInstanceClientInfo.getTaskResultCollecterThreadInfo();
            if (taskThreadInfo == null) {
                setNeedStop(true);
                return null;
            }
            if (taskThreadInfo.getThreadId().equals(batchTaskInstanceExecuterThreadInfo.getThreadId())) {
                taskThreadInfo.setLastUpdateDate(new Date());
                taskThreadInfo.setStatus(batchTaskInstanceExecuterThreadInfo.getStatus());
                return null;
            }
            setNeedStop(true);
            return null;

        } else {
            setNeedStop(true);
            return null;
        }

    }
}
